from django.db import models
from django.utils.timezone import now


# Create your models here.

class Users(models.Model):
    account = models.CharField(max_length=10, unique=True)  # 账户
    belongs = models.CharField(max_length=10, unique=True)  # 归属人
    password = models.CharField(max_length=100, default='123456789')  # 密码
    add_date = models.DateTimeField()  # 录入时间
    status = models.IntegerField(default=0)  # 当前状态。0：使用中，1：失效
    auth = models.IntegerField(default=0)  # 权限。0：最低，1：管理员
    session_id = models.CharField(max_length=40, null=True, blank=False)
    last_ip = models.CharField(max_length=15, default='')  # 最后登录ip
    last_time = models.DateTimeField(blank=True, null=True)  # 最后登录时间


class Auth(models.Model):  # 权限表
    name = models.CharField(max_length=10)  # 权限名称
    desc = models.CharField(max_length=20)  # 权限描述
    level = models.IntegerField(default=1)  # 权限级别
    father = models.ForeignKey('self', on_delete=models.CASCADE, null=True, blank=True)  # 父级权限


class Role(models.Model):  # 角色表
    name = models.CharField(max_length=10)  # 角色名称
    desc = models.CharField(max_length=20)  # 角色描述
    level = models.IntegerField(default=1)  # 权限级别
    father = models.ForeignKey('self', on_delete=models.CASCADE, null=True, blank=True)  # 父级角色
    gen_time = models.DateTimeField(default=now)  # 创建时间

    class Meta:
        ordering = ["-level"]


class RoleAuth(models.Model):
    role = models.ForeignKey('Role', on_delete=models.DO_NOTHING)
    auth = models.ForeignKey('Auth', on_delete=models.DO_NOTHING)

# class UserRole(models.Model):
